package com.多线程基础.锁的常用情况.Lock锁实现;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyThread extends Thread{
    static int ticket=0;
    static Lock lock=new ReentrantLock();
    //同样，锁对象必须是静态

    @Override
    public void run() {//try catch finally必然会被执行
        while (true){
                lock.lock();
            try {
                if(ticket==100){
                    break;
                }else {
                    Thread.sleep(10);
                    ticket++;
                    System.out.println(getName()+"正在买"+ticket+"张");
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);

            } finally {
                lock.unlock();
            }
        }
    }
}
